﻿using LD.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolReadImageFrm : FrmToolBase
    {
        public ToolReadImageFrm()
        {
            InitializeComponent();

            this.Text = TB_Tool.Name;
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += ToolReadImageFrm_Shown;
        }

        private void ToolReadImageFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = Newtonsoft.Json.JsonConvert.SerializeObject(new ReadImageParam()
            {
                Camera = this.cob_camera.Text,
                CameraUsing = this.rab_carmer.Checked,
                ImagePath = this.txt_imagePath.Text,
                ConvertGray = this.chk_gray.Checked,
                CameraName = this.cob_camera.Text,
            });
            TB_Tool.Param = param;
        }

        private void Init()
        {
            if (CameraFactory.Instance.CameraList != null)
            {
                foreach (var item in CameraFactory.Instance.CameraList)
                {
                    this.cob_camera.Items.Add(item.Key);
                }
            }

            if (TB_Tool.Param == null)
                return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ReadImageParam>(TB_Tool.Param);
            if (param != null)
            {
                this.cob_camera.Text = param.Camera;
                this.rab_carmer.Checked = param.CameraUsing;
                this.rab_image.Checked = !param.CameraUsing ? true : false;
                this.txt_imagePath.Text = param.ImagePath;
                this.chk_gray.Checked = param.ConvertGray;
            }

        }

        private void btn_upload_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Title = "请选择图片";
                //ofd.Filter = "(*.bmp)|*.bmp|(*.jpg) | *.jpg";
                ofd.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +
          "*.tiff; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
                ofd.RestoreDirectory = true;
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string fileName = ofd.FileName;
                    this.txt_imagePath.Text = fileName;
                }
            }
        }
    }
}
